How to solve the Error - name is not defined in a function in python